package com.cennavi.bigdata.baotou.scripts;

import com.alibaba.fastjson.JSONObject;
import com.cennavi.bigdata.baotou.util.PropertiesUtil;
import com.mongodb.*;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.io.WKTReader;

import java.util.ArrayList;
import java.util.List;

import static com.mongodb.client.model.Indexes.geo2dsphere;

/**
 * Created by zuoweiguang on 2017/3/9.
 * 脚本
 * 分析决策支持系统：指数分析预警 - 根据rtic坐标获取当前rtic中心的位置，并单独生成一张表，对应rtic_id
 * 生成的表功能：根据商圈区域匹配范围内的所有点，再获取对应rtic的路况状态
 */
public class MakeRticCentroid {

    private WKTReader wktReader = new WKTReader();
    private MongoClient mongoClient = null;
    private DB db = null;
    private DBCollection rticCentroidCol = null;
    private DBCollection trafficStatusCol = null;

    public MakeRticCentroid() {
        //获取 mongo 的连接
        ServerAddress serverAddress = new ServerAddress("15.32.12.63", 27017);
        mongoClient = new MongoClient(serverAddress);
        db = mongoClient.getDB("baotou_traffic");
        trafficStatusCol = db.getCollection("traffic_status");
        rticCentroidCol = db.getCollection("rtic_centroid");

    }

    public void makeRticCentroid() {
        DBCursor cursor = trafficStatusCol.find(new BasicDBObject("level", 17));
        List<DBObject> rticCentroidList = new ArrayList<>();
        try {
            int total = 0;
            while (cursor.hasNext()) {
                try {
                    DBObject rticObj = cursor.next();
                    String rtic_id = (String) rticObj.get("rtic_id");
                    String wkt = (String) rticObj.get("geometry");
                    LineString lineString = (LineString) wktReader.read(wkt);
                    Point centroid = lineString.getCentroid();
                    double x = centroid.getX();
                    double y = centroid.getY();
                    double[] coordinates = new double[2];
                    coordinates[0] = x;
                    coordinates[1] = y;
                    BasicDBObject geojson = new BasicDBObject();
                    geojson.append("type", "Point");
                    geojson.append("coordinates", coordinates);

                    BasicDBObject rticCentroid = new BasicDBObject();
                    rticCentroid.append("rtic_id", rtic_id);
                    rticCentroid.append("rtic_centroid", geojson);
                    rticCentroidList.add(rticCentroid);
                    total += 1;

                    if (total % 500 == 0) {
                        rticCentroidCol.insert(rticCentroidList);
                        rticCentroidList.clear();
                        System.out.println("make rtic centroid count:" + total);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (rticCentroidList.size() > 0) {
                rticCentroidCol.insert(rticCentroidList);
                rticCentroidList.clear();
                System.out.println("make rtic centroid count:" + total);
            }

//            rticCentroidCol.createIndex(new BasicDBObject("rtic_id", 1));
//            rticCentroidCol.createIndex(geo2dsphere("rtic_centroid"));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            mongoClient.close();
        }
    }

    public static void main(String[] args) {
        MakeRticCentroid mrc = new MakeRticCentroid();
        mrc.makeRticCentroid();
    }

}
